[AGC023D]go home

2019-12-06
Atcoder

题意

数轴的整点上有一些人,一辆公交车从S开始载上并送这些人回家,方向取决于车上人的投票

所有的人都会让自己回家的时间最小,求公交车送完所有人的时间

题解

不一定所有人都往自己的方向投票

如果最左边的人小于最右边的人,那么不管怎样公交车都先到最右边,再到最左边(即左边的人一开始投右边)

直接合并,统计答案

调试记录

p爆long long

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cstdio>
#define LL long long
const int maxn = 5e5 + 5;
using namespace std;
int n, s, x[maxn], dire;
LL ans = 0, p[maxn];
int main(){
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i++) scanf("%d%lld", x + i, p + i);
int l = 1, r = n;
while (1){
if (x[l] >= s){
ans += 1ll * (x[r] - s);
break;
}
if (x[r] <= s){
ans += 1ll * (s - x[l]);
break;
}
if (p[l] < p[r]){
if (dire != 1) dire = 1, ans += 1ll * (x[r] - x[l]);
p[r] += p[l++];
}
else if (p[l] >= p[r]){
if (dire != -1) dire = -1, ans += 1ll * (x[r] - x[l]);
p[l] += p[r--];
}
} printf("%lld\n", ans);
return 0;
}